<template>
  <div class="login-wrap">
    <el-form
      class="login-form"
      label-position="top"
      label-width="80px"
      :model="login_data"
    >
      <h2>用户登录</h2>
      <el-form-item label="用户名">
        <el-input
          v-model="login_data.username"
          placeholder="请输入用户名"
          prefix-icon="iconfont icon-yonghu"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input
          type="password"
          placeholder="请输入密码"
          v-model="login_data.password"
          prefix-icon="iconfont icon-suo"
        ></el-input>
      </el-form-item>
      <el-button class="login-btn" type="primary" @click="login"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      login_data: {
        username: "",
        password: "",
      },
    };
  },
  //   created: function () {
  //     self.login();
  //   },
  methods: {
    login: function () {
      let k = this;
      axios({
        method: "post",
        url: "/login",
        dataType: "json",
        data: this.login_data,
      }).then(function (response) {
        if (response.data.msg == "success") {
          k.$message({
            message: "登录成功，正在跳转 ...",
            type: "success",
          });
          window.sessionStorage.setItem("username", k.login_data.username);
          k.$router.push("/welcome");
        } else if (response.data.msg == "unknown user") {
          k.$message.error("用户不存在");
        } else if (response.data.msg == "wrong password") {
          k.$message.error("密码错误");
        } else {
          k.$message.error("登录失败：", response.data.msg);
        }
        k.login_data = {
          username: "",
          password: "",
        };
      });
    },
  },
};
</script>

<style scoped>
.login-wrap {
  width: 100%;
  height: 100%;
  margin: 0;
  background: #324152;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-form {
  width: 400px;
  background: #fff;
  border-radius: 5px;
  padding: 30px;
}
.login-btn {
  width: 100%;
}
</style>

